package ywpt.service.people;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.Department;
import ywpt.model.JiguanInfo;
import ywpt.model.PeopleInfo;
import ywpt.model.PositionInfo;
import ywpt.model.XueliInfo;
import ywpt.web.common.PublicFunction;
@Service("peopleService")
@RemoteProxy(name="peopleService")
public class PeopleServiceImpl extends HibernateEntityDao<PeopleInfo> implements
		PeopleService {
	public PeopleServiceImpl() {

	}

	@Override
	public PeopleInfo getPeopleInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public String createPeopleCode() {
		DetachedCriteria query = DetachedCriteria.forClass(PeopleInfo.class,
				"people");
		String peopleCode = "";
		String code = "";
		Integer maxId = new Integer(0);
		query.addOrder(Order.desc("id"));
		List<PeopleInfo> peopleInfos = this.getPeopleInfos(query);
		if (peopleInfos.iterator().hasNext()) {
			code = peopleInfos.iterator().next().getPeopleCode();
			maxId = new Integer(code);
		}
		peopleCode = PublicFunction.getNextCode("", maxId, false, 4);
		query.setProjection(null);
		return peopleCode;
	}

	@Override
	public int getPeopleInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PeopleInfo> getPeopleInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PeopleInfo> getPeopleInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void savePeopleInfo(PeopleInfo peopleInfo, Long departId,
			Long positionId,Long jiguanId,Long xueliId,Long checkerId) {
		if (departId != null) {
			peopleInfo.setDepart(this.get(Department.class, departId));
		}
		if (positionId != null) {
			peopleInfo.setPosition(this.get(PositionInfo.class, positionId));
		}
		if(jiguanId != null){
			peopleInfo.setJiguan(this.get(JiguanInfo.class, jiguanId));
		}
		if(xueliId != null){
			peopleInfo.setXueli(this.get(XueliInfo.class,xueliId));
		}
		if (checkerId != null) {
			peopleInfo.setChecker(this.get(checkerId));
		}
		super.save(peopleInfo);
	}

	@Override
	public void updatePeopleInfo(PeopleInfo peopleInfo, Long departId,
			Long positionId,Long jiguanId,Long xueliId,Long checkerId) {
		if (departId != null) {
			peopleInfo.setDepart(this.get(Department.class, departId));
		}
		if (positionId != null) {
			peopleInfo.setPosition(this.get(PositionInfo.class, positionId));
		}
		if(jiguanId != null){
			peopleInfo.setJiguan(this.get(JiguanInfo.class, jiguanId));
		}
		if(xueliId != null){
			peopleInfo.setXueli(this.get(XueliInfo.class,xueliId));
		}
		if (checkerId != null) {
			peopleInfo.setChecker(this.get(checkerId));
		}
		this.update(peopleInfo);
	}

	@Override
	public void update(PeopleInfo peopleInfo) {
		this.getHibernateTemplate().save(peopleInfo);
	}

	@Override
	public void removePeopleInfo(Long id) {
		this.removeById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<PeopleInfo> getPeoplesByPeopleName(String peopleName,
			int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(PeopleInfo.class,
				"peopele");
		query.add(Restrictions.isNull("quitDate"));
		// query.add(Restrictions.eq("organType", "01"));
		if (peopleName != null && !peopleName.trim().equals("")) {
			query.add(Restrictions.or(Restrictions.like("peopleName", "%"
					+ peopleName + "%"), Restrictions.like("peopleCode", "%"
					+ peopleName + "%")));
			// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("peopleCode"));
		List<PeopleInfo> peopleInfoList = new ArrayList<PeopleInfo>();
		List<PeopleInfo> dpeopleInfoLists = new ArrayList<PeopleInfo>();
		PeopleInfo peopleInfo = new PeopleInfo();

		peopleInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for (Iterator<PeopleInfo> itr = peopleInfoList.iterator(); itr
				.hasNext(); i++) {
			peopleInfo = itr.next();
			String ss = PublicFunction.format(peopleInfo.getPeopleCode() + "",
					3, "  ", 1)
					+ peopleInfo.getPeopleName();

			peopleInfo.setPeopleName(ss);
			dpeopleInfoLists.add(peopleInfo);
		}
		return dpeopleInfoLists;
	}

	@Override
	@RemoteMethod
	public int getPeoplesCountByName(String peopleName) {
		DetachedCriteria query = DetachedCriteria.forClass(PeopleInfo.class,
				"people");
		query.add(Restrictions.isNull("quitDate"));
		if (peopleName != null && !peopleName.trim().equals("")) {
			query.add(Restrictions.or(Restrictions.like("peopleName", "%"
					+ peopleName + "%"), Restrictions.like("peopleCode", "%"
					+ peopleName + "%")));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public List<PeopleInfo> getPeopleInfos() {
		return this.getAll();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> getDepartments() {
		String hql="select distinct(depart) from PeopleInfo";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PositionInfo> getPositions() {
		String hql="select distinct(position) from PeopleInfo";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<XueliInfo> getXuelis() {
		String hql="select distinct(xueli) from PeopleInfo";
		return this.find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<PeopleInfo> getPeopleInfosByIds(String assistMans) {
		if(assistMans.length()>0)
			assistMans=assistMans.substring(0,assistMans.length()-1);
		String idstr[]=assistMans.split(",");
		Long id[]=new Long[idstr.length];
		for(int i=0;i<idstr.length;i++)
			id[i]=Long.parseLong(idstr[i]);
		DetachedCriteria query=DetachedCriteria.forClass(PeopleInfo.class);
		query.add(Restrictions.in("id",id));
		List<PeopleInfo> list=this.findAllByCriteria(query);
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<PeopleInfo> getPeopleInfosByDepartId(Long departId) {
		DetachedCriteria query=DetachedCriteria.forClass(PeopleInfo.class);
		query.createAlias("depart", "depart").add(Restrictions.eq("depart.id", departId));
		Object[] state = {1,2};
		query.add(Restrictions.in("state",state));
		query.add(Restrictions.isNull("quitDate"));
		List<PeopleInfo> list=this.findAllByCriteria(query);
		return list;
	}
}
